﻿#ifndef GLOBALSETTINGS_H
#define GLOBALSETTINGS_H

#include <QString>
#include "MyTcpClientThread.h"
#include "FrameMgr.h"
#include "qcustomplot.h"

class GlobalSettings
{
public:
    const static unsigned char SAMPLE_HCHANNEL_SUM = 8;
    const static unsigned char SAMPLE_LCHANNEL_SUM = 16;
    const static unsigned char BYTES_PER_HCHANNEL_SAMPLE = 2;
    const static unsigned char BYTES_PER_LCHANNEL_SAMPLE = 2;
    static QColor _curveColor[SAMPLE_LCHANNEL_SUM];

    typedef struct tagHSamplePacket
    {
        unsigned short _sample[SAMPLE_HCHANNEL_SUM];

        void parser(const unsigned char* buf, unsigned char bytesPerSample = 2)
        {
            for(unsigned char i=0;i<SAMPLE_HCHANNEL_SUM;i++)
            {
                _sample[i] = buf[i*bytesPerSample+1];
                _sample[i] <<= 8;
                _sample[i] += buf[i*bytesPerSample];
            }
        }
    }HSamplePacket,*PTR_HSamplePacket;

    typedef struct tagLSamplePacket
    {
        signed short _sample[SAMPLE_LCHANNEL_SUM];

        void parser(const unsigned char* buf, unsigned char bytesPerSample = 2)
        {
            unsigned short temp;
            for(unsigned char i=0;i<SAMPLE_LCHANNEL_SUM;i++)
            {
                temp = buf[i*bytesPerSample+1];
                temp <<= 8;
                temp += buf[i*bytesPerSample];

                _sample[i] = (signed short)temp;
            }
        }
    }LSamplePacket,*PTR_LSamplePacket;

protected:
    static GlobalSettings* _pInstance;
    GlobalSettings();

public:
    QString _serverIP;
    unsigned int _serverPort;

    FrameMgr* _frameMgr;
    MyTcpClientThread* _tcpClientThread;

protected:
    QVector<PTR_HSamplePacket> _hchannelSamples;
    QVector<PTR_LSamplePacket> _lchannelSamples;

protected:
    bool loadFromJson(const QString& jsonFile);

public:
    static GlobalSettings* instance();
    static QString appDir();
    static QString appWorkDir();
    void storeToJson();

    bool appendHChannelSample(const unsigned char* buf);
    bool appendLChannelSample(const unsigned char* buf);
    void clearSamples();
    const QVector<PTR_HSamplePacket>* hchannelSamples() const { return &_hchannelSamples; }
    const QVector<PTR_LSamplePacket>* lchannelSamples() const { return &_lchannelSamples; }

    static void initChartStyle(QCustomPlot* plot);
    static void initCurveColor(QCPCurve* graph, unsigned int index = 0);
};

#endif // GLOBALSETTINGS_H
